from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from django.db import transaction

User = get_user_model()


class Command(BaseCommand):
    help = '创建测试用户数据'

    def handle(self, *args, **options):
        """创建测试用户"""
        test_users = [
            {
                'email': 'hr@test.com',
                'name': 'HR张经理',
                'password': '123456',
                'role': 'hr',
                'phone': '13800138000',
                'is_staff': True,
            },
            {
                'email': 'candidate@test.com',
                'name': '李候选人',
                'password': '123456',
                'role': 'candidate',
                'phone': '13800138001',
            },
            {
                'email': 'hr2@test.com',
                'name': 'HR王主管',
                'password': '123456',
                'role': 'hr',
                'phone': '13800138002',
            },
            {
                'email': 'candidate2@test.com',
                'name': '张候选人',
                'password': '123456',
                'role': 'candidate',
                'phone': '13800138003',
            },
        ]

        with transaction.atomic():
            for user_data in test_users:
                email = user_data['email']
                
                # 检查用户是否已存在
                if User.objects.filter(email=email).exists():
                    self.stdout.write(
                        self.style.WARNING(f'用户 {email} 已存在，跳过创建')
                    )
                    continue
                
                # 创建用户
                user = User.objects.create_user(**user_data)
                
                self.stdout.write(
                    self.style.SUCCESS(
                        f'成功创建用户: {user.name} ({user.email}) - 角色: {user.role}'
                    )
                )
        
        self.stdout.write(
            self.style.SUCCESS('\n测试用户创建完成！')
        )
        
        # 显示登录信息
        self.stdout.write(
            self.style.SUCCESS('\n=== 测试用户登录信息 ===')
        )
        self.stdout.write('HR用户:')
        self.stdout.write('  邮箱: hr@test.com')
        self.stdout.write('  密码: 123456')
        self.stdout.write('  角色: hr')
        self.stdout.write('')
        self.stdout.write('候选人用户:')
        self.stdout.write('  邮箱: candidate@test.com')
        self.stdout.write('  密码: 123456')
        self.stdout.write('  角色: candidate')
        self.stdout.write('')
        self.stdout.write('更多测试用户: hr2@test.com, candidate2@test.com')
        self.stdout.write('密码均为: 123456') 